package com.example.cxz2021606127.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.cxz2021606127.R;
import com.example.cxz2021606127.activity.WebViewActivity;
import com.example.cxz2021606127.bean.NewsInforBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MyRecyclerViewAdapter2 extends RecyclerView.Adapter<MyRecyclerViewAdapter2.MyViewHolder> {
    private List<NewsInforBean.ResultBean.ListBean> list = new ArrayList<>();
    private Context context;


    public MyRecyclerViewAdapter2(List<NewsInforBean.ResultBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview1, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyRecyclerViewAdapter2.MyViewHolder holder, int position) {
        NewsInforBean.ResultBean.ListBean listBean = list.get(position);

        Glide.with(context).load(listBean.getPic()) //图片加载地址
                .placeholder(R.drawable.tupian1)  //加载未完成时显示占位图
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.mipmap.ic_launcher) //加载出错显示的图片
                .into(holder.imageView); //绑定到ImageView控件
        holder.title.setText(listBean.getTitle());
        holder.src.setText(listBean.getSrc());
        holder.time.setText(listBean.getTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), WebViewActivity.class);
                intent.putExtra("url", listBean.getWeburl());
//                Log.i("xc", "onClick: "+listBean.getUrl());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView src;
        private TextView time;
        private ImageView imageView;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_main_title);
            src = itemView.findViewById(R.id.tv_main_src);
            time = itemView.findViewById(R.id.tv_main_time);
            imageView = itemView.findViewById(R.id.iv);
        }
    }
}
